package com.ls.atf.core.web.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import com.ls.atf.core.web.WebPage;
import com.ls.atf.core.web.elements.Button;
import com.ls.atf.core.web.elements.TextInput;
import com.ls.atf.data.objects.AdminUser;

public class LoginPage extends WebPage<LoginPage> {
	
	private static final String PAGE_URL = BASE_URL + "/access/login";

	public LoginPage(WebDriver driver) {
		super(driver);
	}

	@Override
	public LoginPage load() {
		driver.get(PAGE_URL);
		return this;
	}

	@Override
	public boolean isAvailable() {
		return getUsernameInput().isAvailable() &&
				getPasswordInput().isAvailable() &&
				getLoginButton().isAvailable();
	}
	
	public AdminHomePage loginAs(String username, String password) {
		getUsernameInput().inputText(username);
		getPasswordInput().inputText(password);
		getLoginButton().click();
		return new AdminHomePage(driver).waitUntilAvailable();
	}
	
	private TextInput getUsernameInput() {
		return new TextInput(driver, By.id("username"));
	}
	
	private TextInput getPasswordInput() {
		return new TextInput(driver, By.id("password"));
	}
	
	private Button getLoginButton() {
		return new Button(driver, By.name("commit"));
	}

	public AdminHomePage loginAsAdmin(AdminUser admin) {
		return loginAs(admin.getUsername(), admin.getPassword());
	}

}
